package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.DeleteClientEvent;
	import net.optix.ip.events.GetClientsEvent;
	import net.optix.ip.model.ModelLocator;

	public class DeleteClientCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:DeleteClientEvent = DeleteClientEvent( event );
			
			var delegate:UserDelegate 	= 	new UserDelegate( );     
			delegate.deleteClient( this , myEvent.clientID );		
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			var title:String	=	"Result of delete client";
			var message:String;
			
			var result:int;
			try
			{
				result	=	event.result.result;
				
				switch(result)
				{
					case -2:
						message	=	"Access Denied .";
						break;
					case 0:
						message			=	"Program error!";
						break;
					case 1:
						message			=	"Successfully delete the client!";
						var getClients:GetClientsEvent	=	new GetClientsEvent();
						com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getClients);
						break;
				}
			}
			catch(error:Error)
			{
				model.result	=	"Program error!";
			}
			
			model.bSubmit	=	true;
			
			var alert:Alert	=	 
				Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
			alert.width		=	360;
			alert.height	=	130;
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			trace(event.fault);
			model.result	=	"Program error";
			
		}

	}
}